<template>
	<view class="content">
		<view class="item" v-for="item in list" :key="item._id" @click="clickItem(item)">
			<view class="img">
				<image :src="item.avatar || '../../static/logo.png'" mode=""></image>
			</view>
			<view class="right">
				<view class="right-top">
					<view class="top-name">
						{{item.username}}
					</view>
					<view class="top-sex">
						{{formatSex(item.gender)}}
					</view>
				</view>
				<view class="right-bot">
					<view class="bot-tel">
						{{item.mobile}}
					</view>
					<view class="bot-email">
						{{item.email}}
					</view>
				</view>
			</view>
			<button type="warn" @click.stop="delItem(item)">delete</button>
		</view>
		<button type="default" @click="add">添加</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: []
			}
		},
		onLoad() {
			this.queryList()
		},
		onShow() {
			this.queryList()
		},
		//监听下拉刷新动作
		onPullDownRefresh() {
			this.queryList()
		},
		methods: {
			queryList() {
				uni.showLoading({
					title: '请求列表'
				})
				uniCloud.callFunction({
					name: "uploadFile",
					data: {
						funName: 'queryList'
					},
					success: (res) => {
						this.list = res.result.data
					},
					fail: (f) => {
						console.error(f);
					},
					complete() {
						uni.hideLoading()
						uni.stopPullDownRefresh()
					}
				})
			},
			clickItem(item) {
				uni.navigateTo({
					url: `/pages/add/add?id=${item._id}`
				})
			},
			delItem(item) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '确认删除？',
					success: function(res) {
						if (res.confirm) {
							uniCloud.callFunction({
								name: "uploadFile",
								data: {
									funName: 'del',
									id: item._id
								},
								success: (res) => {
									uni.showToast({
										title: res.result.msg
									})
									that.queryList()
								},
								fail: (f) => {
									console.error(f);
								}
							})
						} else if (res.cancel) {
							uni.showToast({
								title: '取消删除'
							})
						}
					}
				});

			},
			add() {
				uni.navigateTo({
					url: '/pages/add/add',
					animationType: 'slide-in-bottom'
				})
			},
			formatSex(e) {
				if (e === 0) {
					return '未知'
				} else if (e === 1) {
					return '男'
				} else if (e === 2) {
					return '女'
				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		padding: 20rpx;

		.item {
			display: flex;
			border-bottom: 1px solid #eee;
			padding: 10rpx;

			.img {
				width: 80rpx;
				height: 80rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.right {
				display: flex;
				flex-direction: column;
				margin-left: 20rpx;

				.right-top {
					display: flex;

					.top-sex {
						margin-left: 30rpx;
					}
				}

				.right-bot {
					display: flex;

					.bot-email {
						margin-left: 30rpx;
					}
				}
			}
		}
	}
</style>
